import Vue from 'vue'
import VueRouter from 'vue-router'
//引入登录界面
import Login from '../views/Login.vue'
//引入后台界面
import index from '../views/index.vue'
//引入欢迎页面
import Welcome from '../views/welcom.vue'
//引入用户管理界面
import sys_user from '../views/system/user.vue'
//引入角色管理界面
import sys_role from '../views/system/role.vue'
//引入菜单管理界面
import sys_emn from '../views/system/emn.vue'
//引入部门管理界面
import sys_bm from '../views/system/bm.vue'


Vue.use(VueRouter)



//路由规则
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/index', component: index, redirect: '/welcome', children: [
      { path: '/welcome', component: Welcome },
      { path: '/system/user', component: sys_user },
      { path: '/system/role', component: sys_role },
      { path: '/system/emn', component: sys_emn },
      { path: '/system/bm', component: sys_bm }
    ]
  }


]

const router = new VueRouter({
  routes
})



export default router

//路由导航守卫
router.beforeEach(async (to, from, next) => {
  console.log(Vue.prototype.$global.menuList);
  //to表示将要访问的路径
  //from表示从哪个路径跳转而来
  //next表示一个函数，放行：next() 强制跳转next("/login")
  if (to.path == '/login') {
    return next();
  } else {
    const token = window.sessionStorage.getItem("token");
    if (!token) {
      return next("/login");
    }
  }
  // Vue.prototype.$global.startLoading();
  await Vue.prototype.$global.getMenu();
  // Vue.prototype.$global.endLoading();
  next();
})